package com.joe.gulishop.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.joe.gulishop.product.dao.CategoryBrandRelationDao;
import com.joe.gulishop.product.entity.CategoryBrandRelationEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joe.common.utils.PageUtils;
import com.joe.common.utils.Query;

import com.joe.gulishop.product.dao.CategoryDao;
import com.joe.gulishop.product.entity.CategoryEntity;
import com.joe.gulishop.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {



    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }
    //查询出所有的哦分类以及子分类
//    @Override
//    public List<CategoryEntity> listWithTree() {
//        //1.查询出所有的分类
//        List<CategoryEntity> entities = super.baseMapper.selectList(null);
//
//        //2.组装成父子的树状结构
////        2.1 展示所有的一级分类
//        List<CategoryEntity> level1Menus = entities.stream().filter((categoryEntity ->
//                categoryEntity.getParentCid() == 0
//        )).map((menu)->{
//            menu.setChildren(getChildrens(menu,entities));
//            return menu;
//        }).sorted(
//             //   Comparator.comparingInt(CategoryEntity::getSort)
//                Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))
//        ).collect(Collectors.toList());
//        return level1Menus;
//    }

//    //通过递归法获取所有菜单的子菜单
//    public List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
//        List<CategoryEntity> children =all.stream().filter(categoryEntity ->
//                categoryEntity.getParentCid()== root.getCatId() ).
//                map(categoryEntity -> {
//                    categoryEntity.setChildren(getChildrens(categoryEntity, all));
//                    return categoryEntity;
//                }).sorted(
//                        (menu1,menu2)-> (menu1.getSort()==null?0:menu1.getSort())- (menu2.getSort()==null?0: menu2.getSort())
//                ).collect(Collectors.toList());
//
//        return children;
//    }

    @Override
    public List<CategoryEntity> listWithTree() {

        //1、查询出所有分类
        List<CategoryEntity> entities = super.baseMapper.selectList(null);

        //2、组装成父子的树形结构

        //2.1)、找到所有一级分类
        List<CategoryEntity> levelMenus = entities.stream()
                .filter(e -> e.getParentCid() == 0)
                .map((menu) -> {
                    menu.setChildren(getChildrens(menu, entities));
                    return menu;
                })
                .sorted((menu, menu2) -> {
                    return (menu.getSort() == null ? 0 : menu.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
                })
                .collect(Collectors.toList());

        return levelMenus;
    }
    //递归查找所有菜单的子菜单
    private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid().equals(root.getCatId());
        }).map(categoryEntity -> {
            //1、找到子菜单(递归)
            categoryEntity.setChildren(getChildrens(categoryEntity, all));
            return categoryEntity;
        }).sorted((menu, menu2) -> {
            //2、菜单的排序
            return (menu.getSort() == null ? 0 : menu.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return children;

    }
    @Override
    public int removeMenuBuIds(List<Long> asList) {
        //TODO 别人正在使用的时候不能进行删除操作
        return baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> list=new ArrayList<>();
        List<Long> parentList=findParentPath(catelogId,list);

        Collections.reverse(parentList);
        return (Long[]) parentList.toArray(new Long[parentList.size()]);
    }

//    更新数据
    @Override
    @Transactional
    public void updateDetail(CategoryEntity category) {
//        首先更新
            categoryBrandRelationDao.updateCategory(category.getCatId(),category.getName());

    }

    public List<Long> findParentPath(Long catelogId,List<Long> paths){

//收集当前节点
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if (byId.getParentCid()!=0){
            findParentPath(byId.getParentCid(),paths);
        }

        return paths;
    }
}